// pages/searchlist/searchlist.js
let utils = require('../../utils/util')

let db = wx.cloud.database({
    env:'linyr-6gp7qo8008360a60'
})

let pet = db.collection('db_pet')
let look = db.collection('db_look')

Page({

    /**
     * 页面的初始数据
     */
    data: {
        searchVal:'',
        searchList:[]
    },



    //去详情页
    async godetail(event){
        // console.log(event);
        let _id = event.currentTarget.dataset.id
        let dt = new Date()
        let look_time = utils.formatTime(dt)
        await look.where({look_id:_id}).get().then(content=>{
            // console.log(content);
            let { data,errMsg } = content
            if(errMsg=='collection.get:ok'){
                if(data.length==0){
                    pet.where({_id}).get().then(content=>{
                        console.log('2.0',content);
                        let { data,errMsg } = content
                        if(errMsg=='collection.get:ok'){
                            let look_img = data[0].pet_coverURL
                            let look_name = data[0].pet_name
                            look.add({
                                data:{
                                    look_id:_id,
                                    look_time,
                                    look_img,
                                    look_name
                                }
                            })
                        }
                    })
                }
            }
        })
        wx.navigateTo({
          url: '../detail/detail?id='+_id,
        })
    },


     //获取搜索词
     goSearchList(event) {
        // console.log(event);
        if (event.detail) {
            this.setData({
                searchVal: event.detail
            })
            // console.log(this.data.searchVal);
        }
    },

    async onSearch() {
        if(this.data.searchVal==''){
            wx.showToast({
              title: '请输入搜索内容',
              icon:'none'
            })
            return;
        }
        wx.showLoading({
          title: '加载中...',
        })
        let v = this.data.searchVal
        if(v=='狗') v = '犬'
        await pet.where({
            pet_name: db.RegExp({
                regexp: v,
            })
        }).get().then(content => {
            // console.log(content);
            let { data,errMsg } = content
            if(errMsg == 'collection.get:ok'){
                this.setData({
                    searchList:data
                })
            }
            wx.hideLoading();
        })

        let arr = JSON.parse(wx.getStorageSync('historyList'))
        if(!arr.includes(this.data.searchVal)){
            arr.push(this.data.searchVal)
            console.log(arr);
            wx.setStorageSync('historyList', JSON.stringify(arr))
        }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            searchVal:options.searchVal
        })
        wx.showLoading({
          title: '加载中...',
        })
        let v = options.searchVal
        if(v=='狗') v = '犬'
        pet.where({
            pet_name: db.RegExp({
                regexp: v,
            })
        }).get().then(content => {
            // console.log("搜索数据==》",content);
            let { data,errMsg } = content
            if(errMsg == 'collection.get:ok'){
                this.setData({
                    searchList:data
                })
            }
            wx.hideLoading();
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})